package com.example.motion.controller;

import com.example.motion.config.ResultT;
import com.example.motion.model.MotionRecord;
import com.example.motion.model.request.MotionRecordRequest;
import com.example.motion.model.request.StepRequest;
import com.example.motion.service.MotionRecordService;
import com.example.motion.vo.MotionRecodeTopVo;
import com.example.motion.vo.MotionRecordVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 运动
 *
 * @author jxf
 * @version 1.0.0
 * @since 1.0.0
 */
@RestController
@RequestMapping("/record")
public class MotionRecordController {

    @Autowired
    private MotionRecordService motionRecordService;

    /**
     * 获取微信步数
     * @param motionRecordRequest
     * @return
     */
    @PostMapping("/motionRecord")
    public ResultT motionRecord(@Validated @RequestBody MotionRecordRequest motionRecordRequest) {
        try {
            //String encryptedData1 = "aTXRyyMXVo9gmRp8HDMQj6jHaXTUfkRQxeoEPr4+Sqnz+HC97r4TaiCFJtQAYCsfUX9kMtdcpEarEojeb76IStxTzrZNBaKirOofbR4WS5zd27ISzLq3pPASzm0AOgZ8MQHIhtpWVjKo7dKfhaoDIJ3n3tMyPYAKkgmYAIwzUMQCH+l4LH/MAdsTFFTtxrCpaTtR+6t62D6yYndbZYe6tev1XoDcwvARjgGD0FNZPkLWGW5u4A1OF7rkwyPh1LYHmnW9EgnDcnGaxQMNCHb0E/ZeHWN6M1TkkaUkyyfCv/um/kIPtTFCjrbcZcQY6qnt6YspSVqYnq138/PEjrWNfGkRBuLemro9W3bLxJrKp7dUgd5mpDtkI7rb44dRmQ1Rhl9kf3IYuWg3CLf5hfN9KFaNwm0sCEh12US+YBROdBDYCbv3Wm7ivfnve+lGueKs3bGngly5mmEIlJ4xrJJwRGhRq2Qr5R7fZxGbvwO2UfS9GHsCUNMaC4gH9Xpb++aEh7gPoHjNaQueyQAC5zBZzHVb3bDAScZL3ZfPU23ZBEPV/FZeSpvM2PtvutzcZVWdQ4mUI6JeGOKxJzXuJ2qlMsA9u0w22dlMFqctDaUrrHMDSohOOBHaBr2f1RT0omwr7SiFYPeQ745RgMVlt6S4cakb/k+D+ePa2utiEpjvKY2N49GP17BgeOaT0NuTP53FMPl5QCTdX7jX2XNWBkttR3meav4TCp7AHRZ1KtsxsKpom6gVAX5YIW2eu3hQIK4xCB+NynJNujN6NSEgzkjaWf+aZWW9CV+BohhjeL5AdfWHSgB9m8ZZ3TFxN7IbhRN7s9AJrtCiYKYxej4OoSOm8+jWAX9wS+VkuVoqRXoLV1zvBv2vpNAZukqw23t6tR1ZfslDUM4o0Kb4ODiSYWd4XTcz4VslEQN/h+46ixAcTtE+TNeGmMjRPBdLLi2GcOf/JR+XWFhnPLvp3Kat6gRmXOA6YMk76E09sZ5QZbzGerMnL+EgK+OODMixEFVBbOoQ46m39t3bODX+EMdXleOatNmUo3gJLvNZJDqHs+rmm94aJD5jpbWHLVOZ/we9wQuqZNNoqi6BCxZmHfI6Krjy6v4TWeTkIud+Sh76Zz4ClNxX3T2iiV3fZ/g43FFhFETZba91POEx4IZqivZCjXK21liD3sI/OO86xnhv8abcfiz72jwISF+YG1D9Im0xxCHA8EpqDL/BAKF4jdTCNnvs9VfD4C0YAC6ulWs1O3ThJyUj6MQIXdjn3Rde3FooMbt8BriBptXWWAY1zbariA29udRBUMckKJ/CGpNH6cpCe8HpJtblVdk85xlHdczL/DSiyVGhYKYkVFvMnSOmo6QHHhxrHa5x+bWlcibchpWk/VfM1OngB2b4YMCKSGaOFRLhomiCgzbygwxPuBce+jhTxr54yvgphJxRZ93PsoeX3VWD5fVkXD0E7kzUIfgzNLtBjNtoGYg5miuPmafTAVUEE/ePqI0gpNCMWAyIkIb1RzzIuZAGBxwEvLOELA3yi5GPOC2A9EfFk950QBE36XkAa4Xkk+H+URNG4Pllny/hiio=";
            MotionRecord motionRecord = motionRecordService.motionRecord(motionRecordRequest.getAppId(), motionRecordRequest.getEncryptedData(),
                    motionRecordRequest.getSessionKey(), motionRecordRequest.getIv(), motionRecordRequest.getOpenid());
            return ResultT.ok(motionRecord);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultT.error();
        }
    }

    /**
     * 兑换步数
     * @param stepRequest
     * @return
     */
    @PostMapping("/exchangeStep")
    public ResultT exchangeStep(@RequestBody StepRequest stepRequest) {
        try {
            MotionRecordVo motionRecordVo = motionRecordService.exchangeStep(stepRequest.getOpenid());
            return ResultT.ok(motionRecordVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultT.error();
        }
    }


    /**
     * 获取首页步数
     * @param openid
     * @return
     */
    @GetMapping("/getStep")
    public ResultT getStep(@RequestParam(value = "openid") String openid) {
        try {
            MotionRecordVo motionRecordVo = motionRecordService.getStep(openid);
            return ResultT.ok(motionRecordVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultT.error();
        }
    }

    /**
     * 获取总步数
     */
    @GetMapping("getTotal")
    public ResultT getTotal() {
        try {
            Map<String, Object> total = motionRecordService.getTotal();
            return ResultT.ok(total);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultT.error();
        }
    }
    /**
     * 获取个人排行信息
     */
    @GetMapping("getPersonTop")
    public ResultT getPersonTop(@RequestParam("pageNo") int pageNo,
                                @RequestParam("pageSize") int pageSize,
                                @RequestParam(value = "officeCode", required = false) String officeCode) {
        try {
            PageInfo<MotionRecodeTopVo> motionRecodeTopVos = motionRecordService.getPersonTop(pageNo, pageSize, officeCode);
            return ResultT.ok(motionRecodeTopVos);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultT.error();
        }
    }

    /**
     * 获取分局排行信息
     */
    @GetMapping("getOfficeTop")
    public ResultT getOfficeTop() {
        try {
            List<MotionRecodeTopVo> motionRecodeTopVos = motionRecordService.getOfficeTop();;
            return ResultT.ok(motionRecodeTopVos);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultT.error();
        }
    }

    /**
     * 获取分局目标里程完成进度
     */
    @GetMapping("getOfficeTargetMileage")
    public ResultT getOfficeTargetMileage() {
        try {
            List<MotionRecodeTopVo> motionRecodeTopVos = motionRecordService.getOfficeTargetMileage();;
            return ResultT.ok(motionRecodeTopVos);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultT.error();
        }
    }

    /**
     * 获取活动参与总人数
     */
    @GetMapping("getAcitivityPersonNum")
    public ResultT getAcitivityPersonNum() {
        try {
            int num = motionRecordService.getAcitivityPersonNum();
            return ResultT.ok(num);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultT.error();
        }
    }

}
